﻿using STHPMS1.BLL.B_Core;
using STHPMS1.ICore.I_BLL.Bus;
using STHPMS1.ICore.I_Entity.Info;
using STHPMS1.ICore.I_Entity.Stmpms1;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq.Expressions;

namespace STHPMS1.BLL.B_Bus
{
    public class S_TrfvolsurveyBus : BllBase, IS_TrfvolsurveyBill
    {
        public void Save<T>(T entity)
            where T : class ,IS_Trfvolsurvey
        {
            if (this.Scene.Dal.Sthpms1.Update(entity) == 0)
            {
                this.Scene.Dal.Sthpms1.Insert(entity);
            }
        }

        public void Remove(IS_Trfvolsurvey entity)
        {
            this.Scene.Dal.Sthpms1.Delete(entity);
        }

        public IEnumerable<T> Get<T>(Expression<Func<T, bool>> predicate, IPagingInfo info)
            where T : class, IS_Trfvolsurvey
        {
            return this.Scene.Dal.Sthpms1.Select<T>(predicate, info,p=>p.Importdate,true);
        }

        public DataTable GetByRoad(string rdwayid, int? direct, DateTime? checkDate, IPagingInfo info)
        {
            int currentPageIndex = info.PageIndex;
            int pageSize = info.PageSize;

            DataTable dt = this.Scene.Dal.Sthpms1.S_Trfvolsurvey.GetTrfvolsurveyByRoad(rdwayid,direct,checkDate);
            DataTable newdt = dt.Clone();

            int rowbegin = currentPageIndex * pageSize;//当前页的第一条数据在dt中的位置
            int rowend = (currentPageIndex + 1) * pageSize;//当前页的最后一条数据在dt中的位置

            if (rowbegin >= dt.Rows.Count)
            {
                return newdt;
            }

            if (rowend > dt.Rows.Count)
            {
                rowend = dt.Rows.Count;
            }

            DataView dv = dt.DefaultView;
            for (int i = rowbegin; i <= rowend - 1; i++)
            {
                newdt.ImportRow(dv[i].Row);
            }

            info.TotalRecords = dt.Rows.Count;

            return newdt;
        }
    }
}
